<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
                * {
            margin: 0;
            padding: 0;
        }
 
        .box {
            width: 312px;
            height: 312px;
            border: 3px solid #000;
            margin: 50px auto 5px;
            font-size: 0;
        }
 
        .box div {
            width: 100px;
            height: 100px;
            display: inline-block;
            border: 2px solid #000;
        }
 
        .d1 {
            background-image: url(./微信图片_20240420132520.jpg);
            background-size: 300px 300px;
            background-position: 0px 0px;
        }
 
 
        #but {
            border: 1px solid #000 transparent;
            width: 300px;
            height: 30px;
            margin: 0 auto;
        }
 
        #but img {
            width: 100px;
            height: 100px;
            float: right;
            display: none;
        }
 
        button {
            margin: 1px auto;
            border: 1px solid #000;
        }
    </style>
</head>
<body>
<div class="box">
    <div id="d1" class="d1"></div>
    <div id="d2" class="d1"></div>
    <div id="d3" class="d1"></div>
    <div id="d4" class="d1"></div>
    <div id="d5" class="d1"></div>
    <div id="d6" class="d1"></div>
    <div id="d7" class="d1"></div>
    <div id="d8" class="d1"></div>
    <div id="d9" class="d1"></div>
</div>
<div id="but">
    <button id="but1">一键完成</button>
    <button id="but2">开始游戏</button>
    <button id="but3">看一眼原图</button>
    <div>
        <img id="yan" src="./微信图片_20240420132520.jpg" alt="">
    </div>
</div>
</body>
<script>
    var data = [['0 0'], ['-100px 0'], ['-200px 0'], ['0 -100px'], ['-100px -100px'], ['-200px -100px'], ['0 -200px'], ['-100px -200px'], ['-200px -200px']]
 
 var but1 = document.getElementById("but1")
 var but2 = document.getElementById("but2")
 var but3 = document.getElementById("but3")
 var yan = document.getElementById("yan")
 var divs = document.querySelectorAll(".d1")

 // 刚打开保持完整
 for (var i = 0; i < divs.length; i++) {
     divs[i].style.backgroundPosition = data[i][0]
 }

 // 一键完成
 but1.addEventListener("click", function () {
     for (var i = 0; i < divs.length; i++) {
         divs[i].style.backgroundPosition = data[i][0]
     }
 })


 // 开始游戏
 but2.addEventListener("click", function () {
var arr = [];
     var maxTimes = 9
     do {
         var num = Math.floor(Math.random() * 9);
         if (-1 == arr.indexOf(num)) {
             arr.push(num);
             maxTimes--;
         }
     } while (maxTimes);

     for (var i = 0; i < divs.length; i++) {
         const k = arr[i]
         divs[k].style.backgroundPosition = data[i][0]
     }

 })

 // 看一眼原图
 but3.addEventListener("mousedown", function () {
     yan.style.display = "block"
 })
 but3.addEventListener("mouseup", function () {
     yan.style.display = "none"
 })



 divs.forEach(function (v, i) {
     v.draggable = "true"

     //开始拖拽的时候触发事件
     v.addEventListener("dragstart", function (e) {
         // console.log('666');
         e.dataTransfer.setData("newdivID",e.target.getAttribute("id"))
         e.dataTransfer.setData("newdiv",e.target.style.backgroundPosition)
     })
     
      // 拖拽松开的时候触发事件
     v.addEventListener("drop",function (e) {
         e.stopPropagation()
         e.preventDefault()
         

         var oldDiv = document.querySelector("#"+e.dataTransfer.getData('newdivID'))//取出并保存开始拖拽的div的id属性的div

         var pos=e.dataTransfer.getData("newdiv") // 取出并保存开始拖拽的div的backgroundPosition属性

         oldDiv.style.backgroundPosition=e.target.style.backgroundPosition//把准备松开到的div的backgroundPosition属性值传递给取出的那个旧div

         e.target.style.backgroundPosition=pos// 把取出的那个旧div的backgroundPosition属性值传递给当前准备松开到的div

     })

     v.addEventListener("dragover",function(e){
         e.preventDefault()
     })

 })
</script>
</html>